package com.ggqq.day330;

public class LongestPrefix {
    public static void main(String[] args) {
        String[] strs = {"flower","flow","flight"};
        LongestPrefix longestPrefix = new LongestPrefix();
        System.out.println(longestPrefix.longestCommonPrefix(strs));

    }
    private String longestCommonPrefix(String[] strs){
        if(strs == null || strs.length==0){
            return "";
        }
        String prefix =strs[0];
        for(int i=1;i<strs.length;i++){
            prefix = longest(prefix,strs[i]);
            if(prefix.length()==0){
                break;
            }
        }
        return prefix;
    }
    private String longest(String s1,String s2){
        int len = Math.min(s1.length(),s2.length());
        int index = 0;
        while(index<len && s1.charAt(index) == s2.charAt(index)){
            index++;
        }
        return s1.substring(0,index);
    }


}
